﻿using UnityEngine;
using System.Collections;

public class Playing : MonoBehaviour {

	// Use this for initialization
	public Transform playerStartPos;
	void Start () {
		if (Network.isServer || Network.isClient) {
			SpawnPlayer();
		}
	}

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            destroyPlayerAndReload();
        }
    }


    private void SpawnPlayer()
    {
        Debug.Log("Spawning Player");
		Network.Instantiate(Resources.Load("Prefabs/Player"), playerStartPos.position, Quaternion.identity, 0);
    }

    private void destroyPlayerAndReload()
    {
        GameObject[] playerObjects = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject playerObject in playerObjects)
        {
            NetworkView networkView = playerObject.GetComponent<NetworkView>();
            if (networkView.isMine)
            {
                Network.RemoveRPCs(networkView.viewID);
                Network.Destroy(playerObject);
            }
        }
        Application.LoadLevel("baseScene");
    }
}
